//
//  WBVisitorView.swift
//  weibo
//
//  Created by 林帆 on 2017/4/7.
//  Copyright © 2017年 林帆. All rights reserved.
//

import UIKit

//访客视图
class WBVisitorView: UIView {

    //注册按钮
     lazy var registerBtn : UIButton = UIButton(
        text: "注册",
        fontSize: 16,
        textColor: #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1))
    //登录按钮
     lazy var loginBtn : UIButton = UIButton(
        text: "登录",
        fontSize: 16,
        textColor: #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1))

    
    // [imageName / message]
    // 如果是首页 imageName == ""
    var visitorInfo : [String : String]? {
        didSet {
            // MARK:  - 设置访客视图信息
            // 使用字典设置
                //        1. 字典信息
                guard let imageName = visitorInfo?["imageName"],
                    let message = visitorInfo?["message"] else {
                    return
                }
                //        2. 设置消息
                tipLabel.text = message
                //        3. 设置图像
                if imageName == "" {
                    startAnimation()
                    return
                }
            
                iconView.image = UIImage(named: imageName)
            
            // 其他控制器
            }
    }
    
    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //旋转图标动画 (首页需要)
    fileprivate func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        // 动画完成不删除， 如果iconview被释放 动画会一起销毁
        anim.isRemovedOnCompletion = false
        
        iconView.layer.add(anim, forKey: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //图像视图
    fileprivate lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "jijian_tubiao-017.jpg"))
    //遮罩图片
    fileprivate lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "class_bgViewi5"))
    // 小房子
    fileprivate lazy var houseIconView : UIImageView = UIImageView(image: UIImage(named: "adduser"))
    //提示标签
    fileprivate lazy var tipLabel = UILabel(
        text: "关注一些人，这样才会有家的归属感，也会有更多的惊喜呢。",
        fontSize: 16,
        textColor: #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1))
    
    
}

extension WBVisitorView {
    func setupUI() {
        backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        
        houseIconView.backgroundColor = #colorLiteral(red: 1, green: 0.2933840834, blue: 0.4420734227, alpha: 1)
        
        //1. 添加控件
        addSubview(iconView)
//        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //文本居中
        tipLabel.textAlignment = .center
        
        //2. 取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //3. 自动布局
        let margin: CGFloat = 20.0
        
        
        /// 1. iconview
        addConstraint(NSLayoutConstraint(item: iconView,//视图
                                         attribute: .centerX,//约束属性
                                         relatedBy: .equal,//约束关系
                                         toItem: self,//参照视图
                                         attribute: .centerX,//参照属性
                                         multiplier: 1.0,//乘积
                                         constant: 0))//约束数值
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 280))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 300))
        
        /// 2. houseIconView
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX, multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY, multiplier: 1.0,
                                         constant: 0))
        
        /// 3. tipLabel
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 240))
        
        /// 4. registerBtn
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        /// 5. loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        /// 6. maskIconView
        //  views: 定义VFL中的控件名称和实际名称映射关系
//        let viewDict = ["maskIconView" : maskIconView]
//        addConstraints(NSLayoutConstraint.constraints(
//            withVisualFormat: "H: | -0-[maskIconView]-0- |",
//            options: [ ],
//            metrics: nil,
//            views: viewDict))
//        addConstraints(NSLayoutConstraint.constraints(
//            withVisualFormat: "V: | -0-[maskIconView]-0- |",
//            options: [ ],
//            metrics: nil,
//            views: viewDict))
    }
}



